package com.party.notify.notifyPush.integral;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.party.core.model.activity.Activity;
import com.party.core.model.commune.CIntegralItem;
import com.party.core.model.commune.CIntegralType;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.message.Message;
import com.party.core.model.message.MessageTag;
import com.party.core.model.notify.TargetTemplate;
import com.party.core.model.order.OrderStatus;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.commune.ICIntegralItemService;
import com.party.core.service.commune.ICIntegralTypeService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.member.IPackageCountService;
import com.party.notify.notifyPush.servce.INotifySendService;
import com.party.notify.notifyPush.servce.INotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by wei.li
 *
 * @date 2017/10/31 0031
 * @time 16:51
 */

@Service
public abstract class INotifyMemberIntegralSendService {

    @Autowired
    private INotifyService notifyService;

    @Autowired
    private INotifySendService notifySendService;

    @Autowired
    private IPackageCountService packageCountService;

    @Autowired
    private ICIntegralTypeService cIntegralTypeService;

    @Autowired
    private ICIntegralItemService cIntegralItemService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IMemberGroupService memberGroupService;

    private ExecutorService threadPool = Executors.newFixedThreadPool(500);


    private static final String MEMBERINTEGRAL_PUSH = "memberIntegralPush";

    /**
     * 获取报名人员列表
     *
     * @return 众筹列表
     */
    public abstract List<Map<String, Object>> getList(String actId, Integer signin);


    /**
     * 消息推送
     *
     * @param map<String,Object> 项目编号
     * @param targetTemplate     模板消息
     * @param targetId           目标编号
     * @param authorId           会员编号
     * @param way                发送方式
     */
    public void toPush(Map<String, Object> map,TargetTemplate targetTemplate,
                       String targetId, String authorId, String way) {


        MemberGroup member = memberGroupService.getPartner(authorId);
        Activity activity = activityService.get(targetId);
        CIntegralType cIntegralType = cIntegralTypeService.get(targetTemplate.getCiTypeId());
        CIntegralItem cIntegralItem = cIntegralItemService.get(targetTemplate.getCiItemId());

        HashMap<String, Object> constant = Maps.newHashMap();
        Member member1 = memberService.get((String) map.get("member"));

        constant.put("{user}", member1.getRealname());
        constant.put("{title}", activity.getTitle());
        constant.put("{typeName}", cIntegralType.getName());
        constant.put("{fraction}", cIntegralItem.getItemFraction());

        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("relId", map.get("id"));
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("orderStatus", OrderStatus.ORDER_STATUS_HAVE_PAID.getCode());
        constant.put("createBy", map.get("id"));
        constant.put("targetId", Strings.nullToEmpty(targetId));
        constant.put("senderId", Strings.nullToEmpty(authorId));
        constant.put("way", way);
        constant.put("{partner}", Strings.nullToEmpty(member.getRealname()));
        constant.put("templateId", targetTemplate.getId());
        notifyService.push(MEMBERINTEGRAL_PUSH, (String) map.get("mobile"),
                authorId, constant, targetTemplate.getTemplate());
    }


    /**
     * 发送短信
     *
     * @param targetTemplate 模板消息
     * @param targetId       目标编号
     * @param authorId       会员编号
     * @param way            发送方式
     */
    public boolean send(TargetTemplate targetTemplate, String targetId, String authorId, String way) {
        List<Map<String, Object>> list = this.getList(targetId, targetTemplate.getMemberType());
        boolean isCanFlag = packageCountService.checkSpareNum(targetTemplate, list.size());
        if (isCanFlag) {
            for (Map<String, Object> mmp : list) {
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        toPush(mmp,targetTemplate, targetId, authorId, way);
                    }
                });
            }
        }
        return isCanFlag;
    }
}
